unit ADToolMainUnit;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ExtCtrls, ComCtrls, GridsEh, DBGridEh, DB, ADODB, StdCtrls,
  Menus, Buttons;

type
  TfrmADToolMain = class(TForm)
    pcAD: TPageControl;
    TabSheet1: TTabSheet;
    TabSheet2: TTabSheet;
    TabSheet3: TTabSheet;
    TabSheet4: TTabSheet;
    Panel1: TPanel;
    DBGridEh1: TDBGridEh;
    Database: TADOConnection;
    QueryMenu: TADOQuery;
    QuerySync: TADOQuery;
    QuerySearch: TADOQuery;
    QueryCommet: TADOQuery;
    DSMenu: TDataSource;
    DSSearch: TDataSource;
    DSSync: TDataSource;
    DSCommet: TDataSource;
    pmMenu: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    BitBtn1: TBitBtn;
    QueryMenuad_id: TIntegerField;
    QueryMenuad_type: TIntegerField;
    QueryMenuad_url: TWideStringField;
    QueryMenuad_res: TBlobField;
    QueryMenuad_memo: TWideStringField;
    QueryMenuad_clicks: TIntegerField;
    QueryMenuad_name: TWideStringField;
    QueryMenuad_version: TWideStringField;
    QuerySearchad_id: TAutoIncField;
    QuerySearchad_type: TIntegerField;
    QuerySearchad_url: TWideStringField;
    QuerySearchad_res: TBlobField;
    QuerySearchad_memo: TWideStringField;
    QuerySearchad_clicks: TIntegerField;
    QuerySearchad_name: TWideStringField;
    QuerySearchad_version: TWideStringField;
    DBGridEh2: TDBGridEh;
    pmSearch: TPopupMenu;
    MenuItem1: TMenuItem;
    MenuItem2: TMenuItem;
    DBGridEh3: TDBGridEh;
    DBGridEh4: TDBGridEh;
    pmCommet: TPopupMenu;
    MenuItem3: TMenuItem;
    MenuItem4: TMenuItem;
    pmSync: TPopupMenu;
    MenuItem5: TMenuItem;
    MenuItem6: TMenuItem;
    QueryCommetad_id: TAutoIncField;
    QueryCommetad_type: TIntegerField;
    QueryCommetad_url: TWideStringField;
    QueryCommetad_res: TBlobField;
    QueryCommetad_memo: TWideStringField;
    QueryCommetad_clicks: TIntegerField;
    QueryCommetad_name: TWideStringField;
    QueryCommetad_version: TWideStringField;
    QuerySyncad_id: TAutoIncField;
    QuerySyncad_type: TIntegerField;
    QuerySyncad_url: TWideStringField;
    QuerySyncad_res: TBlobField;
    QuerySyncad_memo: TWideStringField;
    QuerySyncad_clicks: TIntegerField;
    QuerySyncad_name: TWideStringField;
    QuerySyncad_version: TWideStringField;
    Label1: TLabel;
    edtMenuImageInterval: TEdit;
    udMenu: TUpDown;
    Label2: TLabel;
    Label3: TLabel;
    edtSyncImageInterval: TEdit;
    udSync: TUpDown;
    Label4: TLabel;
    Label5: TLabel;
    edtSearchEngineUrl: TEdit;
    TabSheet5: TTabSheet;
    lblHomepage: TLabel;
    edtHomepage: TEdit;
    lblDesktop: TLabel;
    edtWallpaper: TEdit;
    btnDesktop: TSpeedButton;
    QueryHomepage: TADOQuery;
    DSHomepage: TDataSource;
    QueryWallpaper: TADOQuery;
    DSWallpaper: TDataSource;
    QueryHomepagead_id: TAutoIncField;
    QueryHomepagead_type: TIntegerField;
    QueryHomepagead_url: TWideStringField;
    QueryHomepagead_res: TBlobField;
    QueryHomepagead_memo: TWideStringField;
    QueryHomepagead_clicks: TIntegerField;
    QueryHomepagead_name: TWideStringField;
    QueryHomepagead_version: TWideStringField;
    QueryWallpaperad_id: TAutoIncField;
    QueryWallpaperad_type: TIntegerField;
    QueryWallpaperad_url: TWideStringField;
    QueryWallpaperad_res: TBlobField;
    QueryWallpaperad_memo: TWideStringField;
    QueryWallpaperad_clicks: TIntegerField;
    QueryWallpaperad_name: TWideStringField;
    QueryWallpaperad_version: TWideStringField;
    chkHomepageEnable: TCheckBox;
    chkDesktopEnable: TCheckBox;
    procedure N1Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure BitBtn1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure DBGridEh1Columns1EditButtonClick(Sender: TObject;
      var Handled: Boolean);
    procedure QueryMenuad_urlGetText(Sender: TField; var Text: String;
      DisplayText: Boolean);
    procedure QueryMenuad_urlSetText(Sender: TField; const Text: String);
    procedure MenuItem1Click(Sender: TObject);
    procedure MenuItem2Click(Sender: TObject);
    procedure MenuItem3Click(Sender: TObject);
    procedure MenuItem4Click(Sender: TObject);
    procedure MenuItem5Click(Sender: TObject);
    procedure DBGridEh4Columns1EditButtonClick(Sender: TObject;
      var Handled: Boolean);
    procedure QueryMenuBeforePost(DataSet: TDataSet);
    procedure btnDesktopClick(Sender: TObject);
    procedure MenuItem6Click(Sender: TObject);
    procedure chkHomepageEnableClick(Sender: TObject);
    procedure chkDesktopEnableClick(Sender: TObject);
  private
    { Private declarations }
    procedure LoadData;
    procedure SaveData;
  public
    { Public declarations }
  end;

var
  frmADToolMain: TfrmADToolMain;

implementation

uses ConstUnit, FunctionUnit;

{$R *.dfm}

{ TfrmADToolMain }

procedure TfrmADToolMain.LoadData;
begin
  QueryMenu.Open;
  QuerySync.Open;
  QuerySearch.Open;
  QueryCommet.Open;
  QueryHomepage.Open;
  QueryWallpaper.Open;
  udMenu.Position := StrToIntDef(GetSysParams(MenuImageInterval, Database), 0);
  udSync.Position := StrToIntDef(GetSysParams(SyncImageInterval, Database), 0);
  edtSearchEngineUrl.Text := DecString( GetSysParams(MainSearchEngineUrl, Database) );
  if not QueryHomepage.IsEmpty then
    edtHomepage.Text := DecString( QueryHomepagead_url.Value );
  if not QueryWallpaper.IsEmpty then
    edtWallpaper.Text := DecString( QueryWallpaperad_name.Value );

  chkHomepageEnable.Checked := StrToBoolDef(GetSysParams( ADHomepageEnable, Database ), False);
  chkDesktopEnable.Checked := StrToBoolDef(GetSysParams(ADDesktopEnable, Database), False);
end;

procedure TfrmADToolMain.SaveData;
begin
  QueryMenu.CheckBrowseMode;
  QueryMenu.UpdateBatch();
  QuerySync.CheckBrowseMode;
  QuerySync.UpdateBatch;
  QuerySearch.CheckBrowseMode;
  QuerySearch.UpdateBatch;
  QueryCommet.CheckBrowseMode;
  QueryCommet.UpdateBatch;
  QueryWallpaper.UpdateBatch;

  if QueryHomepage.IsEmpty then
    QueryHomepage.Append
  else
    QueryHomepage.Edit;
  QueryHomepagead_type.Value := Integer(atHomepage);
  QueryHomepagead_url.Value := EncString( edtHomepage.Text );
  QueryHomepage.CheckBrowseMode;
  QueryHomepage.UpdateBatch;
  SetSysParams(MenuImageInterval, IntToStr(udMenu.Position), Database);
  SetSysParams(SyncImageInterval, IntToStr(udSync.Position), Database);
  SetSysParams(MainSearchEngineUrl, EncString(edtSearchEngineUrl.Text), Database);
  SetSysParams(ADDesktopEnable, BoolToStr(chkDesktopEnable.Checked, True), Database);
  SetSysParams(ADHomepageEnable, BoolToStr(chkHomepageEnable.Checked, True), Database);
end;

procedure TfrmADToolMain.N1Click(Sender: TObject);
begin
  QueryMenu.Append;
  QueryMenuad_type.Value := Integer( atMenuImage );
end;

procedure TfrmADToolMain.N2Click(Sender: TObject);
begin
  if not QueryMenu.IsEmpty then
    QueryMenu.Delete;
end;

procedure TfrmADToolMain.BitBtn1Click(Sender: TObject);
begin
  SaveData;
end;

procedure TfrmADToolMain.FormCreate(Sender: TObject);
begin
  Database.ConnectionString := Format(DBFormat, [ExtractFilePath(ParamStr(0)) + 'Data\BarData.mdb']);
  Database.Open;

  LoadData;
  chkHomepageEnableClick(chkHomepageEnable);
  chkDesktopEnableClick(chkDesktopEnable);

  pcAD.ActivePageIndex := 0;
end;

procedure TfrmADToolMain.DBGridEh1Columns1EditButtonClick(Sender: TObject;
  var Handled: Boolean);
var
  ms: TMemoryStream;
begin
  with TOpenDialog.Create(Self) do
  try
    Filter := 'JEGP;GIF|*.jpg;*.jpeg;*.gif';
    if not Execute then
      Exit;

    ms := TMemoryStream.Create;
    try
      ms.LoadFromFile(FileName);
      CompressStream(ms);
      QueryMenu.Edit;
      QueryMenuad_res.LoadFromStream(ms);
      QueryMenuad_name.Value := EncString( ExtractFileName( FileName ) );
      QueryMenu.CheckBrowseMode;
    finally
      ms.Free;
    end;


  finally
    Free;
  end;

end;

procedure TfrmADToolMain.QueryMenuad_urlGetText(Sender: TField;
  var Text: String; DisplayText: Boolean);
begin
  if not Sender.IsNull then
    Text := DecString(Sender.Value);
end;

procedure TfrmADToolMain.QueryMenuad_urlSetText(Sender: TField;
  const Text: String);
begin
  Sender.Value := EncString(Text);
end;

procedure TfrmADToolMain.MenuItem1Click(Sender: TObject);
begin
  QuerySearch.Append;
  QuerySearchad_type.Value := Integer(atSearchButton);
//  QuerySearch.CheckBrowseMode;
end;

procedure TfrmADToolMain.MenuItem2Click(Sender: TObject);
begin
  if not QuerySearch.IsEmpty then
    QuerySearch.Delete;
end;

procedure TfrmADToolMain.MenuItem3Click(Sender: TObject);
begin
  QueryCommet.Append;
  QueryCommetad_type.Value := Integer(atCommetTop10);

end;

procedure TfrmADToolMain.MenuItem4Click(Sender: TObject);
begin
  if not QueryCommet.IsEmpty then
    QueryCommet.Delete;
end;

procedure TfrmADToolMain.MenuItem5Click(Sender: TObject);
begin
  QuerySync.Append;
  QuerySyncad_type.Value := Integer(atSyncImage);
end;

procedure TfrmADToolMain.DBGridEh4Columns1EditButtonClick(Sender: TObject;
  var Handled: Boolean);
var
  ms: TMemoryStream;
begin
  with TOpenDialog.Create(Self) do
  try
    Filter := 'JEGP;GIF|*.jpg;*.jpeg;*.gif';
    if not Execute then
      Exit;

    ms := TMemoryStream.Create;
    try
      ms.LoadFromFile(FileName);
      CompressStream(ms);
      QuerySync.Edit;
      QuerySyncad_res.LoadFromStream(ms);
      QuerySyncad_name.Value := EncString( ExtractFileName( FileName ) );
      QuerySync.CheckBrowseMode;
    finally
      ms.Free;
    end;


  finally
    Free;
  end;

end;

procedure TfrmADToolMain.QueryMenuBeforePost(DataSet: TDataSet);
begin
//  DataSet.DisableControls;
//  try
//    DataSet.First;
//    while not DataSet.Eof do
//    begin
//      DataSet.Edit;
      DataSet.FieldByName('ad_type').Value := DataSet.Tag;
//      DataSet.CheckBrowseMode;
//      DataSet.Next;
//    end;
//  finally
//    DataSet.CheckBrowseMode;
//  end;

end;

procedure TfrmADToolMain.btnDesktopClick(Sender: TObject);
var
  ms: TMemoryStream;
begin
  with TOpenDialog.Create(Self) do
  try
    Filter := 'JPEGͼƬ(*.jpg,*.jpeg)|*.jpg;*.jpeg';
    if not Execute then
      Exit;

    if QueryWallpaper.IsEmpty then
      QueryWallpaper.Append
    else
      QueryWallpaper.Edit;
    QueryWallpaperad_type.Value := Integer(atWallpaper);
    QueryWallpaperad_name.Value := EncString( ExtractFileName(FileName) );
    edtWallpaper.Text := ExtractFileName(FileName);
    ms := TMemoryStream.Create;
    try
      ms.LoadFromFile(FileName);
      CompressStream(ms);
      QueryWallpaperad_res.LoadFromStream(ms);
    finally
      ms.Free;
    end;
    QueryWallpaper.CheckBrowseMode;
  finally
    Free;
  end;

end;

procedure TfrmADToolMain.MenuItem6Click(Sender: TObject);
begin
  if not QuerySync.IsEmpty then
    QuerySync.Delete;
end;

procedure TfrmADToolMain.chkHomepageEnableClick(Sender: TObject);
begin
  lblHomepage.Enabled := chkHomepageEnable.Checked;
  edtHomepage.Enabled := chkHomepageEnable.Checked;
end;

procedure TfrmADToolMain.chkDesktopEnableClick(Sender: TObject);
begin
  lblDesktop.Enabled := chkDesktopEnable.Checked;
  edtWallpaper.Enabled := chkDesktopEnable.Checked;
  btnDesktop.Enabled := chkDesktopEnable.Checked;
end;

end.
